package com.fh.Servlet_exer;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author 方小勤
 * @version 1.0~
 * 3. 使用 idea 开发一个 PigServlet ,要求完成
 * (1) 通过继承 HttpServlet 开发该 Servlet
 * (2) 要求访问 url 为 http://localhost:8080/servlet/pig1 或者 /pig2
 * (3) 当访问时，分别输出 get 和 post 访问的次数(即分开统计) , 并输出浏览器/客户端的 ip
 * (4) 通过注解配置, 使用前面的 login.html, 完成测试
 * (5) 要求 PigServlet, 在 Tomcat 启动时，就自动加载
 */
@WebServlet(urlPatterns = {"/pig1","/pig2"},loadOnStartup=1)
public class home03 extends HttpServlet {
    private int Getcount = 0;
    private int Postcount = 0;
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("init() 被调用");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getRemoteAddr());
        System.out.println("doPost被调用--调用次数："+ ++Postcount);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getRemoteAddr());
        System.out.println("doGet被调用--调用次数："+ ++Getcount);
    }
}
